package com.example.babydatacollector;

import java.util.Date;

import com.example.babydatacollector.EventContract.Event;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	public static final String TYPE_EAT = "eat";
	public static final String TYPE_SLEEP = "sleep";
	public static final String TYPE_PISS = "piss";
	public static final String TYPE_SHIT = "shit";

	private SQLiteDatabase db;
	private Date startTimeEat;
	private Date startTimeSleep;
	private SharedPreferences sharedPref;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setInitialStates();

		EventDbHelper mDbHelper = new EventDbHelper(this);
		db = mDbHelper.getWritableDatabase();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void eat(View view) {
		startTimeEat = handleToggleButtonClicked(view, startTimeEat, TYPE_EAT,
				R.string.checked_key_eat, R.string.start_time_key_eat);
	}

	public void sleep(View view) {
		startTimeSleep = handleToggleButtonClicked(view, startTimeSleep,
				TYPE_SLEEP, R.string.checked_key_sleep,
				R.string.start_time_key_sleep);
	}

	private Date handleToggleButtonClicked(View view, Date startTime,
			String type, int checkedKeyId, int startTimeKeyId) {
		Date newStartTime;
		boolean checked = ((ToggleButton) view).isChecked();
		if (checked) {
			newStartTime = new Date();
		} else {
			record(type, startTime, new Date());
			newStartTime = null;
		}

		String startTimeKey = getString(startTimeKeyId);
		SharedPreferences.Editor editor = sharedPref.edit();
		editor.putBoolean(getString(checkedKeyId), checked);
		if (newStartTime != null) {
			editor.putLong(startTimeKey, newStartTime.getTime());
		} else {
			editor.remove(startTimeKey);
		}
		editor.commit();

		return newStartTime;
	}

	public void piss(View view) {
		record(TYPE_PISS, new Date(), null);
	}

	public void shit(View view) {
		record(TYPE_SHIT, new Date(), null);
	}

	private void record(String type, Date startTime, Date endTime) {
		// Create a new map of values, where column names are the keys
		ContentValues values = new ContentValues();
		values.put(Event.COLUMN_NAME_TYPE, type);
		values.put(Event.COLUMN_NAME_START_TIME, startTime.getTime());
		if (endTime != null) {
			values.put(Event.COLUMN_NAME_END_TIME, endTime.getTime());
		}

		// Insert the new row
		db.insert(Event.TABLE_NAME, null, values);
	}

	public void viewReport(View view) {
		Intent intent = new Intent(this, ReportActivity.class);
		startActivity(intent);
	}

	public void viewChart(View view) {
		Intent intent = new Intent(this, ChartActivity.class);
		startActivity(intent);
	}

	private void setInitialStates() {
		sharedPref = getPreferences(Context.MODE_PRIVATE);

		setToggleButtonChecked(R.id.eat, R.string.checked_key_eat);
		startTimeEat = getStartTime(R.string.start_time_key_eat);

		setToggleButtonChecked(R.id.sleep, R.string.checked_key_sleep);
		startTimeSleep = getStartTime(R.string.start_time_key_sleep);
	}

	private void setToggleButtonChecked(int toggleButtonId, int checkedKeyId) {
		ToggleButton toggleButton = (ToggleButton) findViewById(toggleButtonId);
		boolean checked = sharedPref.getBoolean(getString(checkedKeyId), false);
		toggleButton.setChecked(checked);
	}

	private Date getStartTime(int startTimeKeyId) {
		String startTimeKey = getString(startTimeKeyId);
		if (!sharedPref.contains(startTimeKey)) {
			return null;
		}
		return new Date(sharedPref.getLong(startTimeKey, -1));
	}

}
